package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.activity.DetailsActivity;
import com.jstyle.jclifexd.model.Activitities;
import com.jstyle.jclifexd.model.Createddata;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ActivitvtAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    List<Createddata> Details;
    Context context;
    //私有属性
    private OnItemClickListener onItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(Createddata aa, int position);
    }

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ActivitvtAdapter(Context mycontext) {
        this.context = mycontext;
        this.Details = new ArrayList<>();
    }

    public void SetData(List<Createddata> myDetails) {
        this.Details = myDetails;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_activitysb, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        final Createddata sleepDetail = Details.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
        Glide.with(context)
                .load(sleepDetail.getImgUrl())
                .diskCacheStrategy(DiskCacheStrategy.DATA) // 不使用磁盘缓存
                .override(ScreenUtils.dip2px(context,77.5f),ScreenUtils.dip2px(context,77.5f))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(ScreenUtils.dip2px(context, 11f))))
                .error(context.getResources().getDrawable(R.drawable.hometitle))
                .into(viewHolder.imageview);
                viewHolder.title_name.setText(sleepDetail.getTitle());
                viewHolder.title_time.setText(context.getResources().getString(R.string.times)+DateUtil.gettime(context, sleepDetail.getStart())
                +"-"+DateUtil.gettime(context, sleepDetail.getEnd()));
                viewHolder.title_area.setText(context.getResources().getString(R.string.location)+sleepDetail.getAddress());
      String VALUE=  sleepDetail.getType().equals("act")?context.getResources().getString(R.string.creator):context.getResources().getString(R.string.create_team);
                viewHolder.title_user.setText(VALUE+sleepDetail.getCreaterName());
                viewHolder.setting_count.setText((sleepDetail.getNumber()+"").equals("9999")?context.getResources().getString(R.string.unlimited):sleepDetail.getNumber()+"");
                viewHolder.getting_count.setText(sleepDetail.getJoinNumber()+"");

            if(Details.get(position).getAvailably()==3){//是否已经结束
                viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbulehuise));
                viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.activity_ended));
            }else if(Details.get(position).getAvailably()==0){//已关闭
            viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbulehuise));
            viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.closed));
        } else{
                //是否已经加入
                if(Details.get(position).getState()==1||NetWorkUtil.getUserId().trim().equals(Details.get(position).getCreaterUid()+"")){
                    viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbuleall));
                    viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.have_joined));
                }else{
                    if(Details.get(position).getState()==2){//等待审核
                        viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbulehuise));
                        viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.activity_review));
                    }else if(Details.get(position).getState()==3){//审核未通过
                        viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbulehuise));
                        viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.audit_failed));
                    }else{
                        switch ( (int)Details.get(position).getAvailably()){
                            case 1://活动进行中
                                viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbuleall));
                                viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.activity_in));
                                break;
                            case 4://已满员
                                viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbulehuise));
                                viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.fully_occupied));
                                break;
                            case 5://结束报名
                                viewHolder.title_stustusCom.setBackground(context.getResources().getDrawable(R.drawable.bg_nunberbulehuise));
                                viewHolder.title_stustusCom.setText(context.getResources().getString(R.string.has_expired));
                                break;
                        }
                    }

                }
            }
        if( sleepDetail.getType().equals("pk")){
            viewHolder.title_stustusCom.setEnabled(true);
        }else{
            viewHolder.title_stustusCom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(Details.get(position).getAvailably()==0){//已关闭
                       Toast.makeText(context, context.getResources().getString(R.string.closed), Toast.LENGTH_SHORT).show();
                }else{
                        Intent intent=new Intent(context,DetailsActivity.class);
                        intent.putExtra("Createddata",sleepDetail);
                        context.startActivity(intent);
                    }

                }
            });
        }





       viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(sleepDetail, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == Details ? 0 : Details.size();
    }


    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.img_cearted)
        ImageView imageview;
        @BindView(R.id.title_name)
        TextView title_name;
        @BindView(R.id.title_time)
        TextView title_time;
        @BindView(R.id.title_area)
        TextView title_area;
        @BindView(R.id.title_user)
        TextView title_user;
        @BindView(R.id.setting_count)
        TextView setting_count;
        @BindView(R.id.getting_count)
        TextView getting_count;
        @BindView(R.id.title_stustusCom)
        Button title_stustusCom;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
